package unittest;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.* ;

import com.bodgittandscarper.broker.server.Database;

import suncertify.db.Data;
import suncertify.db.DuplicateKeyException;
import suncertify.db.RecordNotFoundException;
import static org.junit.Assert.* ;


public class DataTest {

	private static Data data;
	
	static {
		try {
			Database database = new Database("db-2x1.db");
			data = new Data(database);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Test
	public void test_createExactRecord() {
		String[] recordData = new String[] {"Sofia", "Herlev", "Developer", "6", "200", "5"}; 
		
		try {
			data.create(recordData);
		} catch (DuplicateKeyException e) {
			fail("Unable to create new record: " + e);
		}
		
		
		try {
			data.create(recordData);
			fail("Cannot create duplicate record");
		} catch (DuplicateKeyException e) {
			//should happen..
		}
		
	}
	
	@Test
	public void test_createDeleteRecord() {
		System.out.println("Test if createRecord return ok") ;
		
		for (int i = 0; i < 5; i++) {

			int recNo = 0;
			try {
				recNo = data.create(new String[] {"aa", "bb", "c", "d", "e", "f" + (i)});
			} catch (DuplicateKeyException e) {
				fail("Record does not exit, but throws: " + e);
			}

			try {
				data.delete(recNo);
			} catch (RecordNotFoundException e) {
				fail("failed to delete record: " + e);
			}
		}

	}



}
